﻿Imports System.Net
Imports System.IO
Imports System.Text
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System.ComponentModel

Public Class SQLManager

    'Dim rootURL As String = "http://marasiganpayroll.comxa.com/"
    Dim rootURL As String = "http://localhost/payroll/"
    'Dim rootURL As String = "http://192.168.0.104/payroll/"

#Region "Loading Worker"
    Dim errorMsg As String = ""

    Private _worker As BackgroundWorker
    ' This is executed on a worker thread and will not make the dialog unresponsive.  If you want
    ' to interact with the dialog (like changing a progress bar or label), you need to use the
    ' worker's ReportProgress() method (see documentation for details)
    Private Sub WorkerDoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
        ' Initialize encoder here
        Try
            Using client = New WebClient()
                Using stream = client.OpenRead("" + rootURL)
                End Using
            End Using
        Catch
            errorMsg = "Server Connection Failed. "
        End Try
    End Sub

    ' This is executed on the UI thread after the work is complete.  It's a good place to either
    ' close the dialog or indicate that the initialization is complete.  It's safe to work with
    ' controls from this event.
    Private Sub WorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
        If errorMsg.Length > 0 Then
            MsgBox(errorMsg)
            errorMsg = ""
        End If
    End Sub

#End Region

    Sub New()
        '_worker = New BackgroundWorker()
        'AddHandler _worker.DoWork, AddressOf WorkerDoWork
        'AddHandler _worker.RunWorkerCompleted, AddressOf WorkerCompleted
    End Sub

    Public Function CheckForInternetConnection() As Boolean
        If errorMsg.Length > 0 Then
            Return False
        Else
            Return True
        End If
    End Function

    Public Function executeQuery(ByVal queryString As String) As String

        Dim queryRequest As String = "query=" & queryString
        queryRequest = queryRequest.Replace("+", "~")
        Dim request As WebRequest = WebRequest.Create(rootURL & "executor/retrieve")
        Dim response As WebResponse
        Dim data As Byte() = Encoding.UTF8.GetBytes(queryRequest)

        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = data.Length

        Using stream As Stream = request.GetRequestStream()
            stream.Write(data, 0, data.Length)
            stream.Close()
        End Using

        response = request.GetResponse()
        Dim sr As New StreamReader(response.GetResponseStream())
        Dim jsonStr As String = sr.ReadToEnd()
        executeQuery = jsonStr
    End Function

    Public Function executeUpdate(ByVal queryString As String) As String
        
        Dim queryRequest As String = "query=" & queryString
        queryRequest = queryRequest.Replace("+", "~")
        Dim request As WebRequest = WebRequest.Create(rootURL & "executor/update")
        Dim response As WebResponse
        Dim data As Byte() = Encoding.UTF8.GetBytes(queryRequest)

        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = data.Length

        Using stream As Stream = request.GetRequestStream()
            stream.Write(data, 0, data.Length)
            stream.Close()
        End Using

        response = request.GetResponse()
        Dim sr As New StreamReader(response.GetResponseStream())

        Dim responseStr As String = sr.ReadToEnd()
        executeUpdate = responseStr
    End Function



End Class
